package com.gdit.zax.servletjsp;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.CourseDao;
import com.gdit.zax.dao.DynamicDao;
import com.gdit.zax.dao.UpLoadDao;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.dao.VideoDao;
import com.gdit.zax.entitys.BaseEntity;
import com.gdit.zax.entitys.Course;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.entitys.VideoInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.FromRequest;
import com.gdit.zax.utils.Repost;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.WebUpload;
import com.gdit.zax.utils.webUtil;

public class EditVideo extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//变量准备
		FromRequest newrequest = new FromRequest(getServletContext(), request, response);
		
		String token = webUtil.getCookie(request, "LoginCookie");
		String flag = newrequest.getParameterString("flag");
		
		String coursename = newrequest.getParameterString("coursename");
		int coursetype = TextUtil.String2int(newrequest.getParameterString("coursetype") ,-1) ;//选择的视频分类
		String coursecontent = newrequest.getParameterString("coursecontent");
		String coursepic;//章节图片路径
		FileItem coursepicfile = (FileItem) newrequest.getParameter("coursepic");
		
		String sectionname = newrequest.getParameterString("sectionname");
		String sectiontype = newrequest.getParameterString("sectiontype") ;//选择的课程分类
		String sectioncontent = newrequest.getParameterString("sectioncontent");
		
		String videoname = newrequest.getParameterString("videoname");
		String videotype = newrequest.getParameterString("videotype") ;//选择的视频分类
		String videocontent = newrequest.getParameterString("videocontent");
		
		String tagsstr = newrequest.getParameterString("tag");

		
		

		
		//数据库，权限检测
		if (coursetype==-1) {
			Repost.message("请选择视频分类!", request, response);
			return;
		}
		
		Connection conn = DBUtil.getConnection();
		if (conn==null) {
			Repost.dberror(request,response);
			return;
		}
		
		DAOFactory dao = DAOFactory.init(conn, request, response);
		UserDao udao = dao.getUserDao();
		UpLoadDao updao = dao.getUploadDao();
		VideoDao vdao = dao.getVideoDao();
		DynamicDao ddao = dao.getDynamicDao();
		CourseDao cdao = dao.getCourseDao();
		
		UserInfo uinfo = udao.getInfo(Constant.LOGINTYPE_WEB, token);
		if (uinfo==null) {
			Repost.nologin(response);
			return;
		}
		
		if (uinfo.getUlevel() < Constant.ULEVEL_teacher) {
			Repost.message("你还没有权限!", request, response);
			return;
		}
		
		//开始
		Course course = cdao.getCourseByName(coursename);
		if (course == null) course = new Course();
		BaseEntity ctype = new BaseEntity();
		
		course.setAnthorid(uinfo.getId());
		course.setName(coursename);
		course.setIntroduction(coursecontent);
		ctype.setId(coursetype);
		//课程图片处理
		if (coursepicfile != null && !"".equals(coursepicfile.getName())) {//选择了图片
			coursepic = System.currentTimeMillis() + "." + WebUpload.getExtensionName(coursepicfile.getName());
			if (WebUpload.save(getServletContext(), coursepicfile, "videopics", coursepic)){
				course.setPic("videopics/" + coursepic);
			}
		}
		
		VideoInfo info = new VideoInfo();
		info.setName(videoname);
		info.setPic(course.getPic());
		info.setCourseid(coursetype);
		
		if (vdao.editVideoInfo(info)) {
			Repost.message("修改成功!", request, response);
		} else {
			Repost.message("修改失败!", request, response);
		}
		

	}

}
